<template>
  <div class="login-container posR">
    <div class="contentBox">
      <div class="loginFormBox">
        <el-form
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          label-position="left"
        >
          <div class="title-container">
            <h3 class="title">用户登录</h3>
          </div>

          <el-form-item prop="username">
            <span class="icon-container">
              <i class="el-icon-s-custom"></i>
            </span>
            <el-input
              ref="username"
              v-model="loginForm.username"
              placeholder="请输入用户名"
              name="username"
              type="text"
              tabindex="1"
              autocomplete="on"
              v-if="loginWays === 1"
            />
          </el-form-item>

          <el-tooltip
            v-model="capsTooltip"
            content="Caps lock is On"
            placement="right"
            manual
          >
            <el-form-item prop="password">
              <span class="icon-container">
                <i class="el-icon-phone"></i>
              </span>
              <el-input
                :key="passwordType"
                ref="password"
                v-model="loginForm.password"
                :type="passwordType"
                v-if="loginWays === 1"
                placeholder="请输入密码"
                name="password"
                tabindex="2"
                autocomplete="on"
                @keyup.native="checkCapslock"
                @blur="capsTooltip = false"
                @keyup.enter.native="handleLogin"
              />
              <span class="show-pwd" @click="showPwd" v-if="loginWays === 1">
              </span>
            </el-form-item>
          </el-tooltip>
          <el-form-item prop="captcha" style="margin-bottom: 0px">
            <span class="icon-container"> <i class="el-icon-phone"></i> </span>
            <el-input
              ref="code"
              v-model="loginForm.captcha"
              placeholder="验证码"
              name="captcha"
              tabindex="2"
              autocomplete="on"
              style="width: 75%"
              @keyup.native="checkCapslock"
              @blur="capsTooltip = false"
              @keyup.enter.native="handleLogin"
            ></el-input>
            <span class="get-code" @click="getCode">
              <span v-if="!codeTimes || codeTimes === 61">获取验证码</span>
              <span v-else>{{ codeTimes }}s后重新获取</span>
            </span>
          </el-form-item>
          <p class="textRight colorOrange mt5">
            <span class="pointer" @click="forgetPassword">忘记密码？</span>
          </p>
          <el-button
            :loading="loading"
            type="primary"
            style="width: 100%; margin-bottom: 30px"
            @click.native.prevent="handleLogin"
            >登录</el-button
          >
        </el-form>
        <span>用户名: admin   密码: 111111  验证码: 1</span>
      </div>
    </div>

    <!-- <vxeDialog
      ref="getValidPasswordDialog"
      :isShowWin.sync="getValidPasswordDialogShow"
      title="密码重置"
      :width="400"
      :height="520"
      top="30%"
    >
      <getValidPassword class="login-container" ref="getValidPassword" slot="dialogContent" @closeValidWindow="closeValidPasswordWindow" :username="loginForm.username"></getValidPassword>
    </vxeDialog> -->
  </div>
</template>

<script>
// import vxeDialog from "@/publicComponents/dialog/vxeDialog";
// import getValidPassword from "./components/getValidPassword.vue";
export default {
  name: "Login",
  //   components: { vxeDialog, getValidPassword },
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入正确的系统账号"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if ((value && value.length < 6) || !value) {
        callback(new Error("密码不得少于6个字符"));
      } else {
        callback();
      }
    };
    // const validatePhone = (rule, value, callback) => {
    //   if (!value) {
    //     callback(new Error('请输入手机号'))
    //   } else {
    //     callback()
    //   }
    // }
    const validateCode = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入验证码"));
      } else {
        callback();
      }
    };
    return {
      getValidPasswordDialogShow: false,
      loginForm: {
        username: "",
        password: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", validator: validateUsername },
        ],
        password: [
          { required: true, trigger: "blur", validator: validatePassword },
        ],
        captcha: [{ required: true, trigger: "blur", validator: validateCode }],
      },
      passwordType: "password",
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {},
      loginWays: 1, // 1---系统账号密码， 2---手机号
      codeTimes: 61, // 剩余多少秒重新获取验证码
      codeTimers: null, // 获取验证码定时器
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        const query = route.query;
        console.log("route", route);
        if (query) {
          this.redirect = query.redirect;
          this.otherQuery = this.getOtherQuery(query);
        }
      },
      immediate: true,
    },
  },
  created() {
    // window.addEventListener('storage', this.afterQRScan)
    // console.log(process.env.NODE_ENV, "process.env");
    this.getCookieLogin();
  },
  mounted() {
    if (this.loginForm.username === "") {
      this.$refs.username.focus();
    } else if (this.loginForm.password === "") {
      this.$refs.password.focus();
    }
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    forgetPassword() {
      this.getValidPasswordDialogShow = true;
    },
    checkCapslock(e) {
      const { key } = e;
      this.capsTooltip = key && key.length === 1 && key >= "A" && key <= "Z";
    },
    showPwd() {
      if (this.passwordType === "password") {
        this.passwordType = "";
      } else {
        this.passwordType = "password";
      }
      this.$nextTick(() => {
        this.$refs.password.focus();
      });
    },
    async getCode() {
      //get-code
      if (this.codeTimers) {
        return;
      } else if (!this.loginForm.username) {
        this.$messageWindow({
          type: "error",
          message: "请输入系统账号",
        });
        return;
      } else if (
        !this.loginForm.password ||
        (this.loginForm.password && this.loginForm.password.length < 6)
      ) {
        this.$messageWindow({
          type: "error",
          message: "密码不得少于6个字符",
        });
        return;
      }
      var _timer = true;
      //   await getCaptcha({ username: this.loginForm.username, password: this.loginForm.password }).catch(err => {
      //     _timer = false
      //   });
      if (!_timer) return;
      this.codeTimes = 60;
      this.codeTimers = setInterval(() => {
        if (this.codeTimes === 0) {
          clearInterval(this.codeTimers);
          this.codeTimes = 61;
          this.codeTimers = null;
        } else {
          this.codeTimes--;
        }
      }, 1000);
    },
    getCookieLogin() {},
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          //   this.loading = true;
          this.$store.dispatch("login").then(() => {
            this.loading = false;
            this.$router.push({
              path: this.redirect || "/",
              query: this.otherQuery,
            });
          });
        }
      });
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== "redirect") {
          acc[cur] = query[cur];
        }
        return acc;
      }, {});
    },
    changeLogin() {
      // if (this.loginWays === 1) {
      //   this.loginWays = 2
      //   this.loginForm = {
      //     phone: '',
      //     code: '',
      //   }
      // } else {
      //   this.loginWays = 1
      //   this.loginForm = {
      //     username: '',
      //     password: '',
      //   }
      // }
    },
    closeValidPasswordWindow() {
      this.getValidPasswordDialogShow = false;
    },
  },
  beforeDestroy() {
    clearInterval(this.codeTimers);
    this.codeTimers = null;
  },
};
</script>

<style lang="less">
@bg: #283443;
@light_gray: #fff;
@cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: @cursor)) {
  .login-container .el-input input {
    color: @cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-form-item__content {
    height: auto;
    border: 1px solid #e0e0e0;
    height: 46px;
    border-radius: 6px;
  }
  .el-input {
    display: inline-block;
    // height: 42px;
    width: 100%;
    position: absolute;
    top: 2px;
    left: 0px;
    padding-left: 30px;
    box-sizing: border-box;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      // padding: 12px 5px 12px 15px;
      color: #454545;
      height: 42px;
      line-height: 42px;
      // caret-color: @cursor;
      box-sizing: border-box !important;
      // }
    }
    input:-ms-input-placeholder,
    input::-moz-placeholder,
    input::-webkit-input-placeholder {
      color: #454545;
    }
    input:-webkit-autofill {
      -webkit-text-fill-color: #454545;
      -webkit-box-shadow: 0 0 0 1000px #fff inset;
      // -webkit-animation: autofill-fix1 1s infinite;
      // background-color: transparent;
      // background-image: none !important;
    }
  }

  .el-form-item {
    // border: 1px solid rgba(255, 255, 255, 0.1);
    margin-bottom: 22px !important;
    background: #fff;
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="less" scoped>
@bg: #2d3a4b;
@dark_gray: #889aa4;
@light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  overflow: hidden;

  .contentBox {
    position: absolute;
    z-index: 3;
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    .loginFormBox {
      width: 40%;
      display: flex;
      // justify-content: center;
      align-items: center;
      img {
        display: inline-block;
      }
    }
    .loginFormBox {
      justify-content: flex-end;
    }
  }
  .loginFormBox {
    overflow: hidden;
    .login-form {
      width: 22%;
      height: 50%;
      min-height: 460px;
      min-width: 360px;
      background-color: #f0f0f0;
      border-radius: 10px;
      padding: 30px 50px;
      box-sizing: border-box;
      //   box-shadow: 3px 3px 5px #889aa4;
    }
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .icon-container {
    position: absolute;
    top: 3px;
    left: 0px;
    padding: 6px 5px 6px 15px;
    color: @dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 30px;
      color: #333;
      margin: 30px auto 40px auto;
      text-align: center;
      font-weight: bold;
      line-height: 40px;
    }

    .set-language {
      color: #fff;
      position: absolute;
      top: 3px;
      font-size: 18px;
      right: 0px;
      cursor: pointer;
    }
    .change-login {
      color: #fff;
      top: 50px;
      font-size: 14px;
      position: absolute;
      cursor: pointer;
      right: 0;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: @dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .get-code {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 14px;
    color: @dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>